import os
import secrets
from typing import Any, Dict, List, Optional, Union

from pydantic import AnyHttpUrl, validator
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    API_V1_STR: str = "/api/v1"
    SECRET_KEY: str = secrets.token_urlsafe(32)
    # 60 minutes * 24 hours * 8 days = 8 days
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
    # BACKEND_CORS_ORIGINS is a comma-separated list of origins
    BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []

    @validator("BACKEND_CORS_ORIGINS", pre=True)
    def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
        if isinstance(v, str) and not v.startswith("["):
            return [i.strip() for i in v.split(",")]
        elif isinstance(v, (list, str)):
            return v
        raise ValueError(v)

    PROJECT_NAME: str
    
    DATABASE_URL: str
    
    # 微信设置
    WECHAT_DATA_DIR: str = "./wechat_data"
    
    # 日志设置
    LOG_LEVEL: str = "INFO"
    LOG_FILE: str = "./logs/wechat_assistant.log"
    
    # Model MGT 设置
    MODEL_MGT_BASE_URL: str = "http://127.0.0.1:8001"
    MODEL_MGT_DEFAULT_MODEL: str = "deepseek-r1"
    MODEL_MGT_DEFAULT_SCENARIO: str = "general_chat"
    MODEL_MGT_DEFAULT_STRATEGY: str = "响应最快"
    
    # 功能开关
    USE_LLM_FOR_PUA_DETECTION: bool = True  # 是否使用大语言模型进行PUA检测
    USE_LLM_FOR_MESSAGE_OPTIMIZATION: bool = True  # 是否使用大语言模型进行消息优化

    class Config:
        case_sensitive = True
        env_file = ".env"


settings = Settings()